<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <link rel="stylesheet" href="../css/public.css">
</head>

<body>
    <!-- createPattern 在指定的方向内重复指定的元素-->

    <img src="../img/header2.png" id="lamp" style="width:50px;height:50px;" />
    <canvas id="myCanvas1" width="300" height="150" style="border:1px solid #d3d3d3;"></canvas>
    <!--<button onclick="draw('repeat')">Repeat</button>
    <button onclick="draw('repeat-x')">Repeat-x</button>
    <button onclick="draw('repeat-y')">Repeat-y</button>
    <button onclick="draw('no-repeat')">No-repeat</button>-->

    <script>
        var a = document.getElementById('myCanvas1');
        var atx = a.getContext('2d');
        var img = document.getElementById("lamp");
        var pat = atx.createPattern(img, "repeat");
        atx.fillStyle = pat;
        atx.fillRect(20, 20, 150, 100);
        atx.fill();

        //动态切换
        // function draw(direction) {
        //     var c = document.getElementById("myCanvas1");
        //     var ctx = c.getContext("2d");
        //     ctx.clearRect(0, 0, c.width, c.height);
        //     var img = document.getElementById("lamp")
        //     var pat = ctx.createPattern(img, direction);
        //     ctx.fillRect(0, 0, 100, 100);
        //     ctx.fillStyle = pat;
        //     ctx.fill();
        // }
    </script>
</body>

</html>